package com.sjh.controller;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sjh.service.IType_service;
import com.sjh.vo.Bis_nurse_bag;
import com.sjh.vo.Bis_nurse_type;

@Controller
public class Type_controller {
	
		@Autowired
		private IType_service type_service;
		
		int a=10000;
		
		//查询所有加模糊查询
		@RequestMapping("typeSel")
		public String typeSel(Map<String, Object> map,Bis_nurse_type type){
			List<Map> list = type_service.typeSel(type);
			map.put("list", list);
			return "sjh/selectType";
		}
		
		//删除
		@RequestMapping("typedel/{type_id}")
		public String typedel(@PathVariable String type_id){
			type_service.typedel(type_id);
			return "redirect:/typeSel"; 
		}
		//根据id查询一条信息
		@RequestMapping("typeupdByid/{type_id}")
		public void typeupdByid(@PathVariable String type_id,Model model,HttpServletResponse res){
			ObjectMapper mapper=new ObjectMapper();
			try {
				String jsonStr=mapper.writeValueAsString(type_service.typeupdByid(type_id));
				getWirte(res, jsonStr);
			} catch (JsonProcessingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		public void getWirte(HttpServletResponse resp,String data) {
			try {
				if(data!=null&&!data.equals("")) {
					PrintWriter	out = resp.getWriter();
					out.print(data);
					out.flush();
					out.close();
					return;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		};
		//修改
		@RequestMapping("typeupd")
		public String typeupd(Bis_nurse_type type){
			/*System.out.println("=================="+type.getType_name());*/
			if (type.getType_id().equals("")||type.getType_id()==null) {
				SimpleDateFormat sdf=new SimpleDateFormat("yyMMddhhmm");
				String str=sdf.format(new Date());			
				a++;
				type.setType_id(str+a+"");
				type_service.typeadd(type);
				return "redirect:/typeSel";
			}else {
				type_service.typeupd(type);
				return "redirect:/typeSel";
			}
			
		}
}
